package com.springapp.db;

import com.springapp.mvc.config.SecurityConfiguration;
import com.springapp.db.util.TestHelper;
import com.springapp.mvc.config.SpringMvcConfiguration;
import com.springapp.mvc.entity.Comment;
import com.springapp.mvc.entity.Post;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.jdbc.JdbcTestUtils;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;

/**
 * Created by dbychkov on 23.12.2014.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = {SpringMvcConfiguration.class, SecurityConfiguration.class})
@TransactionConfiguration(transactionManager="txMgr", defaultRollback=true)
@Transactional
public class DbTestsBase {

    @Autowired
    protected JdbcTemplate template;

    protected TestHelper helper;

    @PostConstruct
    public void init(){
        helper = new TestHelper(template);
    }

    protected long countRows(String tableName){
        return JdbcTestUtils.countRowsInTable(template, tableName);
    }

    protected long countRowsWhere(String tableName, String whereClause){
        return JdbcTestUtils.countRowsInTableWhere(template, tableName, whereClause);
    }

    protected Post storePost(Post p){
        return helper.storePost(p);
    }

    protected Comment storeComment(Comment c){
        return helper.storeCommemt(c);

    }

}
